/*
 * Copyright (c) 2020. bmy
 */

interface state {
  TabsContentArr: Array<{
    title: string,
    url: string,
    closable: boolean,
    icon: string
  }>,
  TabsActive: string,
  isLogin: boolean,
  AsideMenu: Array<{
    web_href: string,
    web_title: string,
    icon: string,
    children: Array<{
      web_href: string,
      web_title: string,
      icon: string,
    }>
  }>
}

export default {
  state: {
    isLogin: false,
    TabsContentArr: [
      { title: '系统首页', url: '/', closable: false, icon: 'el-icon-house' },
    ],
    TabsActive: '/',
    AsideMenu: [
      {
        web_href: '/', web_title: '系统首页', icon: 'el-icon-house',
        children: []
      },
      {
        web_href: '/System', web_title: '系统设置', icon: 'el-icon-setting',
        children: [
          { web_href: '/System/MenuConfig', web_title: '菜单配置', icon: 'el-icon-tickets' },
          { web_href: '/System/wardenList', web_title: '管理员列表', icon: 'el-icon-user' },
        ]
      },
      {
        web_href: '/Jurisdiction', web_title: '权限管理', icon: 'el-icon-lock',
        children: [
          { web_href: '/Jurisdiction/RolePermissions', web_title: '角色权限', icon: 'el-icon-key' },
        ]
      }
    ]
  },
  mutations: {
    // 保存顶部菜单
    SET_TABCONTENT: (state: state, activeMenu: string) => {
      // 检查是否已经添加过了
      let s = state.TabsContentArr.filter(function (val: any, index: number) {
        return activeMenu == val.url
      });
      // 如果没有添加过
      if (s.length == 0) {
        state.AsideMenu.forEach((v, i) => {
          // 如果等于，表示一级菜单
          if (activeMenu == v.web_href) {
            state.TabsContentArr.push({
              title: v.web_title,
              url: v.web_href,
              icon: v.icon,
              closable: true
            })
          } else {
            v.children.forEach((va, ix) => {
              if (activeMenu == va.web_href) {
                state.TabsContentArr.push({
                  title: va.web_title,
                  url: va.web_href,
                  icon: va.icon,
                  closable: true
                })
              }
            })
          }
        })

      }
    },
    // 切换激活
    SET_TABACTIVE: (state: state, active: string) => {
      state.TabsActive = active
    },
    // 移除激活
    SET_REMOVETABS: (state: state, targetName: string) => {
      let tabs = state.TabsContentArr;
      let activeName = state.TabsActive;
      // 如果删除的是当前激活菜单
      if (activeName === targetName) {
        tabs.forEach((tab: { [key: string]: any }, index: number) => {
          if (tab.url === targetName) {
            let nextTab: { [key: string]: any } = tabs[index + 1] || tabs[index - 1];
            if (nextTab) {
              activeName = nextTab.url;
            }
          }
        });
      }
      state.TabsActive = activeName;
      state.TabsContentArr = tabs.filter((tab: { [key: string]: any }) => {
        return tab.url !== targetName
      });
    },
    // 修改状态
    SET_LOGINSTATUS: (state: state, status: boolean) => {
      state.isLogin = status
    }
  },

}
